<?php
declare (strict_types=1);

namespace app\common\listener;

use app\common\model\Relation;
use app\common\model\User;

class UserTotal
{
    public function handle($order)
    {
        $user = User::where('id', $order->uid)->field('id,my_total,team_total')->find();
        $amount = $order->real_amount;

        $user->my_total += $amount;
        $user->save();

        if($parents = Relation::where('new_uid', $user->id)->column('uid')){
            User::whereIn('id', $parents)->inc('team_total', floatval($amount))->update();
        }
    }
}